package com.itboye.ihomebank.adapter;

import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.bean.WoDeFangYuanBean;
import com.itboye.ihomebank.constants.NetPublicConstant;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.home.ActivityRenZheng;
import com.itboye.ihomebank.home.ActivityXuanZuKe;
import com.itboye.ihomebank.presenter.HousePresenter;
import com.itboye.ihomebank.support.commonadapter.MutiplyCommonAdapter;
import com.itboye.ihomebank.support.commonadapter.ViewHolder;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;
import com.itboye.ihomebank.util.XImageLoader;

/**
 * 我的房源
 * 
 */
public class WodeFangyYuanAdapter extends
		MutiplyCommonAdapter<WoDeFangYuanBean> implements Observer {
	Activity activity;
	HousePresenter housePresenter;
	protected String uid;

	public WodeFangyYuanAdapter(Activity context, List<WoDeFangYuanBean> datas,
			int... layoutId) {
		super(context, datas, layoutId);
		activity = context;
		housePresenter = new HousePresenter(this);
		uid = SPUtils.get(activity, null, SPContants.USER_ID, "") + "";
		// TODO Auto-generated constructor stub
	}

	@Override
	public int getItemViewType(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void getItemPosition(int position) {
		// TODO Auto-generated method stub

	}

	@Override
	public void convert(ViewHolder holder, final WoDeFangYuanBean t, int type,
			int position) {
		if(t!=null){
			
			holder.setText(R.id.house_name, t.getTitle());
			
			holder.setText(R.id.addr, t.getAddressDetail());
			holder.setText(R.id.house_price, t.getRent() + "元/月");
			XImageLoader.load(NetPublicConstant.IMAGE_URL + t.getImgId(),
					(ImageView) holder.getView(R.id.img_top));

			String biao01 = t.getTags();
			String[] str = {};
			if (biao01 != null) {
				str = biao01.split(",");
			}
		}
		
		// holder.removeAllViews(R.id.fabu_flowLayout02);
		// for (int i = 0; i < str.length; i++) {
		// View view = LayoutInflater.from(MyApplcation.ctx).inflate(
		// R.layout.item_biaoqian, null);
		// final TextView btn_name = (TextView) view
		// .findViewById(R.id.btn_name);
		// btn_name.setText(str[i]);
		//
		// holder.addView(R.id.fabu_flowLayout02, view);
		// }
		holder.setOnClickListener(R.id.shuaxin, position,
				new OnClickListener() {

					@Override
					public void onClick(View v) {

					}
				});
		holder.setOnClickListener(R.id.renzheng, position,
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Intent intent = new Intent(activity,
								ActivityRenZheng.class);
						activity.startActivity(intent);
					}
				});
		holder.setOnClickListener(R.id.xiajia, position, new OnClickListener() {

			@Override
			public void onClick(View v) {
				housePresenter.xiajia(uid, t.getHouseNo());
			}
		});
		holder.setOnClickListener(R.id.xuanzuke, position,
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Intent intent = new Intent(activity,
								ActivityXuanZuKe.class);
						intent.putExtra("house_no", t.getHouseNo());
						activity.startActivity(intent);
						Log.v("house_NO", "zuke"+t.getHouseNo());
					}
				});

	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == HousePresenter.xiaJia_success) {
				ByAlert.alert(entity.getData());
			} else if (entity.getEventType() == HousePresenter.xiaJia_fail) {
				ByAlert.alert(entity.getData());
			}
		}
	}

}
